<?php 
/*
 * 芝麻获取接口模型
 * 
 */
namespace Home\Model;
header("Content-Type:text/html;charset=utf-8");

use Common\Library\BusinessException;
use Common\Library\Constant;

use Think\Cache\Driver\Redis;
use Think\Log;
use Think\Model;

class ZmModel{
    //查看是否授权
    protected function zm_http_post($url,$xml_data){
        $header[] = "content-type:application/json;charset=utf8";
        // $header[] = 'josn: {"uid":"1","sid":"0123456789ABCDEF0123456789ABCDEF","request":{},"ver":"1"}';
        $ch=curl_init($url);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        //curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
        
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($xml_data));
        $response = curl_exec($ch);
        if(curl_errno($ch))
        {
            //print curl_error($ch);
        }
        curl_close($ch);
        Log::write("发送查询：".json_encode($response, JSON_UNESCAPED_UNICODE), Log::INFO);
        return $response;
    }
	
	
	
	//数据反馈
	public function data_back ($xml_data) {
		$sign = $this->genSign($xml_data);
        $url = 'https://api.baiqishi.com/credit/zhima/search';
        //$xml_data['sign'] = $sign;
        $a=$this->zm_http_post($url,$xml_data);
        return $a;
	}
	
	
	
	
    //返回授权
    public function checkauth($xml_data)
    {
        
        $sign = $this->genSign($xml_data);
        $url = 'https://api.baiqishi.com/credit/zhima/search';
        //$xml_data['sign'] = $sign;
        $a=$this->zm_http_post($url,$xml_data);
        return $a;
    }
    //已有openid 直接查询芝麻分
    public function getzhimascore($xml_data2){
        $sign = $this->genSign($xml_data2);
        $url = 'https://api.baiqishi.com/credit/zhima/search';
        //$xml_data['sign'] = $sign;
        $a=$this->zm_http_post($url,$xml_data2);
        return $a;
    }
    
    //获得Openid
    public function getzhimaopenid($xml_data3){
        $sign = $this->genSign($xml_data3);
        $url = 'https://api.baiqishi.com/credit/zhima/search';
        //$xml_data['sign'] = $sign;
        $a=$this->zm_http_post($url,$xml_data3);
        return $a;
    }
    
    public function genSign(array $param) {
        //0. 删除原数据中自带的sign值,防止干扰计算结果
        unset($param['sign']);
    
        //1. 按key由a到z排序
        ksort($param);
    
        foreach ($param as $key => $value) {
            if (is_array($value)) {
                $param[$key] = $this->genSign($value);
            }
        }
    
        //2. 生成以&符链接的key=value形式的字符串
        $paramString = urldecode(http_build_query($param));
    
        //3. 拼接我们的服务秘钥，并md5加密
        $sign = md5($paramString . 'secret@huoyan.com');
    
        //echo "string:" . $paramString, "<br>", "param:", var_dump($param), "sign:", $sign, "\n\n";
    
        return $sign;
    }
    
    function decodeUnicode($str) { 
        return preg_replace_callback('/\\\\u([0-9a-f]{4})/i', 
               create_function( '$matches', 'return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE");' ), 
        $str); 
    }
}
?>